#if defined(ARDUINO) && ARDUINO >=100
  #include "Arduino.h"
 #else
  #include "WProgram.h"
#endif
#include "RS485.h"
int N,P,K;
float tem;
float hum;
float ph;
int ec;  

int RS485::readN(){
  uint8_t Data[10] = { 0 };
  uint8_t ch = 0;
  bool flag = 1;
  while (flag) {
    delay(100);
    Serial.write(Com, 8);
    delay(10);
    if (readBytes(&ch, 1) == 1) {
      if (ch == 0x01) {
        Data[0] = ch;
        if (readBytes(&ch, 1) == 1) {
          if (ch == 0x03) {
            Data[1] = ch;
            if (readBytes(&ch, 1) == 1) {
              if (ch == 0x02) {
                Data[2] = ch;
                if (readBytes(&Data[3], 4) == 4) {
                  if (CRC16_2(Data, 5) == (Data[5] * 256 + Data[6])) {
                    N = Data[3] * 256 + Data[4];
                    flag = 0;
                  }
                }
              }
            }
          }
        }
      }
    }
    Serial.flush();
  }
  return N;
}
int RS485::readP(){
  uint8_t Data1[10] = { 0 };
  uint8_t ch1 = 0;
  bool flag1 = 1;
  while (flag1) {
    delay(100);
    Serial.write(Com1, 8);
    delay(10);
    if (readBytes(&ch1, 1) == 1) {
      if (ch1 == 0x01) {
        Data1[0] = ch1;
        if (readBytes(&ch1, 1) == 1) {
          if (ch1 == 0x03) {
            Data1[1] = ch1;
            if (readBytes(&ch1, 1) == 1) {
              if (ch1 == 0x02) {
                Data1[2] = ch1;
                if (readBytes(&Data1[3], 4) == 4) {
                  if (CRC16_2(Data1, 5) == (Data1[5] * 256 + Data1[6])) {
                    P = Data1[3] * 256 + Data1[4];
                    flag1 = 0;
                  }
                }
              }
            }
          }
        }
      }
    }
    Serial.flush();
  }
  return P;
}
int RS485::readK(){
  uint8_t Data2[10] = { 0 };
  uint8_t ch2 = 0;
  bool flag2 = 1;
  while (flag2) {
    delay(100);
    Serial.write(Com2, 8);
    delay(10);
    if (readBytes(&ch2, 1) == 1) {
      if (ch2 == 0x01) {
        Data2[0] = ch2;
        if (readBytes(&ch2, 1) == 1) {
          if (ch2 == 0x03) {
            Data2[1] = ch2;
            if (readBytes(&ch2, 1) == 1) {
              if (ch2 == 0x02) {
                Data2[2] = ch2;
                if (readBytes(&Data2[3], 4) == 4) {
                  if (CRC16_2(Data2, 5) == (Data2[5] * 256 + Data2[6])) {
                    K = Data2[3] * 256 + Data2[4];
                    flag2 = 0;
                  }
                }
              }
            }
          }
        }
      }
    }
    Serial.flush();
  }
  return K;
}
uint8_t RS485::readBytes(uint8_t *buf, size_t len){
  size_t offset = 0, left = len;
  int16_t Tineout = 500;
  uint8_t *buffer = buf;
  long curr = millis();
  while (left) {
    if (Serial.available()) {
      buffer[offset] = Serial.read();
      offset++;
      left--;
    }
    if (millis() - curr > Tineout) {
      break;
    }
  }
  return offset;
} 
unsigned int RS485::CRC16_2(unsigned char *buf, int len){
  unsigned int crc = 0xFFFF;
  for (int pos = 0; pos < len; pos++) {
    crc ^= (unsigned int)buf[pos];
    for (int i = 8; i != 0; i--) {
      if ((crc & 0x0001) != 0) {
        crc >>= 1;
        crc ^= 0xA001;
      } else {
        crc >>= 1;
      }
    }
  }

  crc = ((crc & 0x00ff) << 8) | ((crc & 0xff00) >> 8);
  return crc;
}


int RS485::readHumiture_Hum(){
  uint8_t Data[10] = { 0 };
  uint8_t ch = 0;
  bool flaga = 1;
  while (flaga) {
    delay(100);
    Serial.write(Com3, 8);
    delay(10);
    if (readBytes(&ch, 1) == 1) {
      if (ch == 0x01) {
        Data[0] = ch;
        if (readBytes(&ch, 1) == 1) {
          if (ch == 0x03) {
            Data[1] = ch;
            if (readBytes(&ch, 1) == 1) {
              if (ch == 0x04) {
                Data[2] = ch;
                if (readBytes(&Data[3], 6) == 6) {
                  if (CRC16_2(Data, 7) == (Data[7] * 256 + Data[8])) {
                    hum = (Data[3] * 256 + Data[4]) / 10.00;
                    flaga = 0;
                  }
                }
              }
            }
          }
        }
      }
    }
    Serial.flush();
  }
  return hum;
}

int RS485::readHumiture_Tem(){
  uint8_t Data[10] = { 0 };
  uint8_t ch = 0;
  bool flagb = 1;
  while (flagb) {
    delay(100);
    Serial.write(Com3, 8);
    delay(10);
    if (readBytes(&ch, 1) == 1) {
      if (ch == 0x01) {
        Data[0] = ch;
        if (readBytes(&ch, 1) == 1) {
          if (ch == 0x03) {
            Data[1] = ch;
            if (readBytes(&ch, 1) == 1) {
              if (ch == 0x04) {
                Data[2] = ch;
                if (readBytes(&Data[3], 6) == 6) {
                  if (CRC16_2(Data, 7) == (Data[7] * 256 + Data[8])) {                
                    tem = (Data[5] * 256 + Data[6]) / 10.00;
                    flagb = 0;
                  }
                }
              }
            }
          }
        }
      }
    }
    Serial.flush();
  }
  return tem;
}

int RS485::readECPH_Ec(){
  uint8_t Data[12] = { 0 };
  uint8_t ch = 0;
  bool flagc = 1;
  while (flagc) {
    delay(100);
    Serial.write(Com4, 8);
    delay(10);
    if (readBytes(&ch, 1) == 1) {
      if (ch == 0x01) {
        Data[0] = ch;
        if (readBytes(&ch, 1) == 1) {
          if (ch == 0x03) {
            Data[1] = ch;
            if (readBytes(&ch, 1) == 1) {
              if (ch == 0x08) {
                Data[2] = ch;
                if (readBytes(&Data[3], 10) == 10) {
                  if (CRC16_2(Data, 11) == (Data[11] * 256 + Data[12])) {
                    ec = Data[7] * 256 + Data[8];                    
                    flagc = 0;
                  }
                }
              }
            }
          }
        }
      }
    }
    Serial.flush();
  }
  return ec;
 }
int RS485::readECPH_Ph(){
  uint8_t Data[12] = { 0 };
  uint8_t ch = 0;
  bool flagd = 1;
  while (flagd) {
    delay(100);
    Serial.write(Com4, 8);
    delay(10);
    if (readBytes(&ch, 1) == 1) {
      if (ch == 0x01) {
        Data[0] = ch;
        if (readBytes(&ch, 1) == 1) {
          if (ch == 0x03) {
            Data[1] = ch;
            if (readBytes(&ch, 1) == 1) {
              if (ch == 0x08) {
                Data[2] = ch;
                if (readBytes(&Data[3], 10) == 10) {
                  if (CRC16_2(Data, 11) == (Data[11] * 256 + Data[12])) {                    
                    ph = (Data[9] * 256 + Data[10]) /10.00;
                    flagd = 0;
                  }
                }
              }
            }
          }
        }
      }
    }
    Serial.flush();
  }
  return ph;
 }
int RS485::readHumitureEC_Hum(){
  uint8_t Data[10] = { 0 };
  uint8_t ch = 0;
  bool flagt = 1;
  while (flagt) {
    delay(100);
    Serial.write(Com5, 8);
    delay(10);
    if (readBytes(&ch, 1) == 1) {
      if (ch == 0x01) {
        Data[0] = ch;
        if (readBytes(&ch, 1) == 1) {
          if (ch == 0x03) {
            Data[1] = ch;
            if (readBytes(&ch, 1) == 1) {
              if (ch == 0x06) {
                Data[2] = ch;
                if (readBytes(&Data[3], 8) == 8) {
                  if (CRC16_2(Data, 9) == (Data[9] * 256 + Data[10])) {
                    hum = (Data[3] * 256 + Data[4]) / 10.00;
                    flagt = 0;
                  }
                }
              }
            }
          }
        }
      }
    }
    Serial.flush();
  }
  return hum;
}

int RS485::readHumitureEC_Tem(){
  uint8_t Data[10] = { 0 };
  uint8_t ch = 0;
  bool flage = 1;
  while (flage) {
    delay(100);
    Serial.write(Com5, 8);
    delay(10);
    if (readBytes(&ch, 1) == 1) {
      if (ch == 0x01) {
        Data[0] = ch;
        if (readBytes(&ch, 1) == 1) {
          if (ch == 0x03) {
            Data[1] = ch;
            if (readBytes(&ch, 1) == 1) {
              if (ch == 0x06) {
                Data[2] = ch;
                if (readBytes(&Data[3], 8) == 8) {
                  if (CRC16_2(Data, 9) == (Data[9] * 256 + Data[10])) {                   
                    tem = (Data[5] * 256 + Data[6]) / 10.00;                    
                    flage = 0;
                  }
                }
              }
            }
          }
        }
      }
    }
    Serial.flush();
  }
  return tem;
}
int RS485::readHumitureEC_Ec(){
  uint8_t Data[10] = { 0 };
  uint8_t ch = 0;
  bool flagh = 1;
  while (flagh) {
    delay(100);
    Serial.write(Com5, 8);
    delay(10);
    if (readBytes(&ch, 1) == 1) {
      if (ch == 0x01) {
        Data[0] = ch;
        if (readBytes(&ch, 1) == 1) {
          if (ch == 0x03) {
            Data[1] = ch;
            if (readBytes(&ch, 1) == 1) {
              if (ch == 0x06) {
                Data[2] = ch;
                if (readBytes(&Data[3], 8) == 8) {
                  if (CRC16_2(Data, 9) == (Data[9] * 256 + Data[10])) {                    
                    ec = Data[7] * 256 + Data[8];
                    flagh = 0;
                  }
                }
              }
            }
          }
        }
      }
    }
    Serial.flush();
  }
  return ec;
}